//
//  XBTagListView.m
//  xunbao
//
//  Created by 程顺晓 on 16/4/6.
//  Copyright © 2016年 itcast. All rights reserved.
//

#import "XBTagListView.h"
#import "XBTaglist.h"
#import "UIView+SDAutoLayout.h"

@interface XBTagListView ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic, strong) void (^myBlock)();
/**
 *  存放所有标签，只有name
 */
@property (nonatomic, strong) NSMutableArray *tags;
/**
    存放所有标签，包含id和name
 */
@property (nonatomic, strong) NSMutableArray *tagLists;
/**
 *  箭头按钮
 */
@property (nonatomic, strong) UIButton *btn;

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewCell *cell;

@property (nonatomic, assign) BOOL *flag;
@end

@implementation XBTagListView

- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super initWithCoder:aDecoder]) {
        [self setUI];
        /**
         *  添加箭头按钮
         */
        [self setBtn];
        
    }
    return self;
}
-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setUI];
        /**
         *  添加箭头按钮
         */
        [self setBtn];
    }
    return self;
}
/**
 *  设置CollectionView的UI
 */
- (void)setUI{

    [XBTaglist loadUrlStringsuccess:^(NSArray *arr) {
        //获取数据中所有的tagLists，然后添加全部在前面，赋值给tagLists，包含id和name
        NSMutableArray *desArr = [NSMutableArray array];
        [desArr addObject:@{@"id":@"all",@"name":@"全部"}];
        [desArr addObjectsFromArray:arr];
        self.tagLists = desArr;
        
        //遍历上面的数组，把只有name标签的存到tags里
        NSMutableArray *arrayM = [NSMutableArray array];
        [desArr enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [arrayM addObject:obj[@"name"]];
        }];
        self.tags = arrayM;
        
        [self.collectionView reloadData];
    }];
    
    //定义一个宽带，适配宽度
    CGFloat width = 0;
    if ([UIScreen mainScreen].bounds.size.width == 414) {
        width = 320;
    }else if([UIScreen mainScreen].bounds.size.width == 375){
        width = 295;
    }else{
        width = 230;
    }
    
//    NSLog(@"%f",width);
    

    //获取流水布局
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(20, 0, width, 165) collectionViewLayout:layout];
    //设置弹簧效果没有
    collectionView.bounces = NO;
    //设置不能滚动
    collectionView.scrollEnabled = NO;
    //设置不展示进度条
    collectionView.showsVerticalScrollIndicator = NO;
    collectionView.backgroundColor = [UIColor whiteColor];
    
    //布局
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 10;
    layout.itemSize = CGSizeMake(50, 30);
    collectionView.contentSize = CGSizeMake(width, 100);
    layout.sectionInset = UIEdgeInsetsMake(7, 0, 10, 0);
    
    //注册cell，尽量都注册
    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
    collectionView.dataSource = self;
    collectionView.delegate = self;
    [self addSubview:collectionView];
}
/**
 *  添加箭头按钮
 */
- (void)setBtn{
    //创建一个箭头按钮
    UIButton *btn = [[UIButton alloc]init];
    [btn setImage:[UIImage imageNamed:@"icon_menu_down"] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"icon_menu_up"] forState:UIControlStateSelected];
    self.btn = btn;
    
    //监听箭头的点击事件
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:btn];
    
    btn.sd_layout.rightSpaceToView(self,10).heightIs(44).widthIs(44).topEqualToView(self);
}
/**
 *  监听按钮的点击事件
 */
- (void)btnClick:(UIButton *)btn{
    
    btn.selected = !btn.selected;
    if (btn.selected) {
        self.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 165);
    }else{
        self.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44);
    }
    self.clipsToBounds = YES;
}
#pragma mark - collectionView 的数据源方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    //因为self.tagLists开始是没有值得，先让他等于1，然后刷新后有值后再等于self.tagLists.count
    return self.tagLists.count == 0 ? 1: self.tagLists.count;
}
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
    self.collectionView = collectionView;
    self.cell = cell;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

    //创建一个label，设置基本属性
    UILabel *tagLable = [[UILabel alloc]initWithFrame:CGRectMake(-2, 3, cell.bounds.size.width+3, 25)];
    tagLable.textColor = [UIColor whiteColor];
    tagLable.backgroundColor = [UIColor colorWithWhite:197.0/255.0 alpha:1];
    tagLable.textAlignment = NSTextAlignmentCenter;
    tagLable.text = self.tags[indexPath.item];
    tagLable.font = [UIFont systemFontOfSize:12];

    [cell addSubview:tagLable];
    
//    if (self.flag==NO) {
    
        if (indexPath.item==0) {
            tagLable.backgroundColor = [UIColor orangeColor];}
//        }else if(indexPath.item ==1){
//            tagLable.backgroundColor = [UIColor orangeColor];
//        }
//        self.flag = YES;
//    }
    
    return cell;
}
#pragma  mark - UICollectionViewDelegate 方法
//点击cell
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
//    NSLog(@"%zd",indexPath.item);
    NSString *title = self.tags[indexPath.item];
    
    if (indexPath.item > 1) {
        [self.tags removeObject:self.tags[indexPath.item]];
        
        [self.tags insertObject:title atIndex:1];
    }
    
    

    
    if (indexPath.item >= 0) {
        [self.tagLists enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([title isEqualToString:obj[@"name"] ]) {
                
                self.tagListBlock(obj[@"id"]);
            }
        }];
    }
    
    
    //刷新数据
    [collectionView reloadData];
    
    //根据按钮的点击事件，来决定点击后view的高度是否要改变
    if (self.btn.isSelected) {
        self.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44);
        self.btn.selected = !self.btn.selected;
    }
}

@end
